package eu.vfs.localfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import eu.vfs.RandomAccess;
import eu.vfs.VirtualFile;

class LocalFile implements VirtualFile {

	File file = null;
	
	VirtualFile parent = null;
	
	boolean isParentSet = false;
	
	public LocalFile root = null;
	
	LocalFile(File file, LocalFile root) {
		this.file = file;
		this.root = root;
	}
	
	LocalFile(String path, LocalFile root) {
		this.file = new File(path);
		this.root = root;
	}

	@Override
	public boolean canRead() {
		return file.canRead();
	}

	@Override
	public boolean canWrite() {
		return file.canWrite();
	}

	@Override
	public boolean createNewFile() throws IOException {
		return file.createNewFile();
	}

	@Override
	public boolean delete() {
		return file.delete();
	}

	@Override
	public String getPath() {
		if (this == root)
			return "/";
		String path = root.getPath()+file.getAbsolutePath().replace(root.getFile().getAbsolutePath(), "");
		return path.replaceAll("\\\\","/").replace("//", "/");
	}

	@Override
	public String getName() {
		return file.getName();
	}

	@Override
	public String getParent() {
		if (isParentSet)
			return parent.getName();
		return file.getParent();
	}

	@Override
	public VirtualFile getParentFile() {
		if (isParentSet)
			return parent;
		File parent = file.getParentFile();
		if (parent == null)
			return null;
		return new LocalFile(parent, root);
	}

	@Override
	public boolean isDirectory() {
		return file.isDirectory();
	}

	@Override
	public boolean isFile() {
		return file.isFile();
	}

	@Override
	public boolean isHidden() {
		return file.isHidden();
	}

	@Override
	public long size() {
		return file.length();
	}

	@Override
	public String[] list() {
		if (!file.isDirectory())
			return null;
		return file.list();
	}

	@Override
	public VirtualFile[] listFiles() {
		if (!file.isDirectory())
			return null;
		File[] files = file.listFiles();
		LocalFile[] children = new LocalFile[files.length];
		for (int i = 0; i < children.length; i++) {
			children[i] = new LocalFile(files[i], root);
			children[i].setParent(this);
		}
		return children;
	}

	@Override
	public boolean mkdir() {
		return file.mkdir();
	}

	@Override
	public boolean mkdirs() {
		return file.mkdirs();
	}

	@Override
	public boolean renameTo(VirtualFile file) {
		//TODO change this
		return this.file.renameTo(new File(file.getPath()));
	}

	@Override
	public boolean setLastModified(long time) {
		return file.setLastModified(time);
	}

	@Override
	public boolean setReadOnly() {
		return file.setReadOnly();
	}

	@Override
	public boolean setReadable(boolean arg0) {
		return file.setReadable(arg0);
	}

	@Override
	public boolean setWritable(boolean arg0) {
		return file.setWritable(arg0);
	}

	@Override
	public java.io.InputStream getInputStream() throws FileNotFoundException {
		return new FileInputStream(file);
	}

	@Override
	public java.io.OutputStream getOutputStream() throws FileNotFoundException {
		return new FileOutputStream(file);
	}

	@Override
	public RandomAccess getRandomAccess() {
		return null;
	}

	@Override
	public long lastModified() {
		return 0;
	}

	void setParent(VirtualFile parent) {
		this.parent = parent;
		this.isParentSet = true;
	}
	
	File getFile() {
		return file;
	}

	void setRoot(LocalFile root) {
		this.root = root;
	}

}
